<?php
class Admin_CategoryController extends Zendvn_Controller_Action{
	
	private $_arrParam;
	private $_mainAction;
	
	public function init(){
		$this->loadTemplate(TEMPLATE_PATH .'/admin');
		
		$this->view->headScript()->appendFile(TEMPLATE_URL .'/admin/js/form.js');
		$this->view->headScript()->appendFile(TEMPLATE_URL .'/admin/js/submit.js');
		$this->_arrParam = $this->_request->getParams();
		$this->_mainAction = $this->_arrParam['module'] . '/' . $this->_arrParam['controller']. '/index';
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
		$tblCategory = new Model_Table_category();
		$this->view->Items = $tblCategory->allItems();	
	}
	public function addAction(){		
		$tblCategory = new Model_Table_category();
		$this->view->level1 = $tblCategory->getLevel1($this->_arrParam);
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateCategory($this->_arrParam);
			if($validator->isValid() == true){
				//Hien thong bao loi
				$this->view->arrError = $validator->getErrors();
				$this->view->arrData = $validator->getData();
			}else{
				$tblCategory->addItem($this->_arrParam);
				$this->_redirect($this->_mainAction);
			}
		}		
	}
	public function deleteAction(){
		$tblCategory = new Model_Table_category();
		$tblCategory->deleteItem($this->_arrParam);
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function editAction(){
		$tblCategory = new Model_Table_category();
		$this->view->level1 = $tblCategory->getLevel1($this->_arrParam);
		$this->view->Item = $tblCategory->getItem($this->_arrParam);
		if($this->_request->isPost()){
			$validator = new Admin_Form_ValidateCategory($this->_arrParam);
			if($validator->isValid() == true){
				//Hien thong bao loi
				$this->view->arrError = $validator->getErrors();
				$this->view->arrData = $validator->getData();
			}else{
				$tblCategory->editItem($this->_arrParam);
				$this->_redirect($this->_mainAction);
			}
			
		}
	}
	public function statusAction(){
		$tblCategory = new Model_Table_category();
		$newStatus = $tblCategory->changeStatus($this->_arrParam);
		
		$lnkImage = APPLICATION_URL . '/public/templates/admin/images/';
		$lnkStatus = APPLICATION_URL .'/admin/category/status/id/'.$this->_arrParam['id'].
										'/status/'. $newStatus;
		if($newStatus == 1){			
			echo '<span class="pointer" onclick=loadPage("' . $this->_arrParam['id'] . '","'. $lnkStatus .'") ><img src="'.$lnkImage.'active.png"></span>';
		}else{
			echo '<span class="pointer" onclick=loadPage("' . $this->_arrParam['id'] . '","'. $lnkStatus .'") ><img src="'.$lnkImage.'inactive.png"></span>';
		}
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}
}